package com.cyf.plc.di.module

import com.cyf.plc.data.remote.CityService
import com.cyf.plc.data.remote.LogService
import com.cyf.plc.data.remote.Pm25Service
import com.cyf.plc.data.remote.WeatherService
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor

import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import javax.inject.Singleton

@Module
class NetApiModule {

    @Singleton
    @Provides
    fun provideMoshi() = Moshi.Builder().add(KotlinJsonAdapterFactory())
        .build()

    @Singleton
    @Provides
    fun provideCityService(): CityService {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY //这里可以选择拦截级别

        val builder: OkHttpClient.Builder = OkHttpClient.Builder()
        builder.addInterceptor(loggingInterceptor)

        return Retrofit.Builder()
            .baseUrl(CityService.GET_CITY_URL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(builder.build())
            .build()
            .create(CityService::class.java)
    }

    @Singleton
    @Provides
    fun provideLogService(): LogService {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY //这里可以选择拦截级别

        val builder: OkHttpClient.Builder = OkHttpClient.Builder()
        builder.addInterceptor(loggingInterceptor)

        return Retrofit.Builder()
            .baseUrl(LogService.BASE_URL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(builder.build())
            .build()
            .create(LogService::class.java)
    }

    @Singleton
    @Provides
    fun provideWeatherService(): WeatherService {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY //这里可以选择拦截级别

        val builder: OkHttpClient.Builder = OkHttpClient.Builder()
        builder.addInterceptor(loggingInterceptor)

        return Retrofit.Builder()
            .baseUrl(WeatherService.GET_WEATHER_URL_OPEN_API)
            .addConverterFactory(
                MoshiConverterFactory.create(
                    Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
                )
            )
            .client(builder.build())
            .build()
            .create(WeatherService::class.java)
    }

    @Singleton
    @Provides
    fun providePm25Service(): Pm25Service {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY //这里可以选择拦截级别

        val builder: OkHttpClient.Builder = OkHttpClient.Builder()
        builder.addInterceptor(loggingInterceptor)

        return Retrofit.Builder()
            .baseUrl(Pm25Service.GET_PM25_URL)
            .addConverterFactory(
                MoshiConverterFactory.create(
                    Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
                )
            )
            .client(builder.build())
            .build()
            .create(Pm25Service::class.java)
    }
}
